package com.zhkj.txg.module.mine.adapter

import android.graphics.Paint
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.zhkj.lib.utils.AccountManager
import com.zhkj.lib.utils.SpanUtil
import com.zhkj.txg.R
import com.zhkj.txg.module.find.ui.FindArticleDetailActivity
import com.zhkj.txg.module.mine.entity.PublishEntity
import com.zhkj.txg.module.product.ui.ProductDetailActivity
import com.zhkj.txg.utils.ImageUtils

class PublishAdapter : BaseQuickAdapter<PublishEntity, BaseViewHolder>(R.layout.item_publish), LoadMoreModule {

    init {
        addChildClickViewIds(R.id.viewPublishGoods)
        setOnItemClickListener { _, _, position ->
            FindArticleDetailActivity.openDetail(context, data[position].id.toInt())
        }
        setOnItemChildClickListener { _, view, position ->
            when (view.id) {
                R.id.viewPublishGoods -> data[position].goods?.let {
                    ProductDetailActivity.normalProduct(context, it.id.toInt())
                }
            }
        }
    }

    override fun convert(helper: BaseViewHolder, item: PublishEntity) {
        val imageView = helper.setText(R.id.tvPublishName, item.content)
            .setText(
                R.id.tvPublishTip,
                context.getString(if (item.goods != null) R.string.publish_goods else R.string.publish_goods_non)
            ).setGone(R.id.viewPublishGoods, item.goods == null)
            .getView<ImageView>(R.id.ivPublish)
        ImageUtils.loadRoundCornerImage(context, imageView, item.images)
        item.goods?.let {
            val goodsImg = helper.setText(R.id.tvOrderGoodsName, it.goodsName)
                .setText(R.id.tvOrderGoodsPrice, SpanUtil.setMoneySymbol(it.price,true))
                .getView<ImageView>(R.id.ivOrderGoods)
            ImageUtils.loadRoundCornerImage(context, goodsImg, it.image)
            // 设置是显示市场价还是显示分享赚多少钱
//            with(helper.getView<TextView>(R.id.tvOrderGoodsEarn)) {
//                if (AccountManager.isMember() && it.shareEarn.isNotEmpty() && it.shareEarn != "0") {
//                    setTextColor(ContextCompat.getColor(context, R.color.colorMain))
//                    paint.flags = Paint.ANTI_ALIAS_FLAG
//                    text = SpanUtil.setPrefixMoneyStr(price = it.shareEarn)
//                } else {
//                    setTextColor(ContextCompat.getColor(context, R.color.colorLightText))
//                    paint.flags = Paint.STRIKE_THRU_TEXT_FLAG
//                    paint.isAntiAlias = true
//                    text = SpanUtil.setMoneySymbol(it.marketPrice)
//                }
//            }
        }
    }
}